//
//  YHHoemAgentDetailsSubViewController.m
//  YHApp
//
//  Created by -- on 2021/7/25.
//

#import "HAHoemAgentDetailsSubVC.h"
#import "YWAlertView.h"
#import "HAChooseTabPopView.h"

@interface HAHoemAgentDetailsSubVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) NSString *date;

@property (nonatomic,strong) id <YWAlertViewProtocol>dateAlert;
/***/
@property (nonatomic, strong) NSDictionary *achievement;

@end

@implementation HAHoemAgentDetailsSubVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    
    if (self.type == 1) {
        self.date = [NSString currentTimeWith:@"yyyy"];
    }else if (self.type == 2) {
        self.date = [NSString currentTimeWith:@"yyyy-MM"];
    }else if (self.type == 3) {
        self.date = [NSString currentTimeWith:@"yyyy-MM-dd"];
    }
    
    [self.dataArray removeAllObjects];
    [self.dataArray addObject:@"时间"];
    [self.dataArray addObject:@"机具总数"];
    [self.dataArray addObject:@"装机机具数"];
    [self.dataArray addObject:@"库存机具数"];
    [self.dataArray addObject:@"代理总数"];
    [self.dataArray addObject:@"商户总数"];
    [self.tableView reloadData];
    
    MJRefreshGifHeader *mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header = mj_header;

    [self.tableView.mj_header beginRefreshing];
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)initUI{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

-(void)loadNewData {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.type == 1) {
        [dic setValue:@"year" forKey:@"type"];
    }else if (self.type == 2) {
        [dic setValue:@"month" forKey:@"type"];
    }else if (self.type == 3) {
        [dic setValue:@"day" forKey:@"type"];
    }
    [dic setValue:self.date forKey:@"value"];
    [dic setValue:self.agent_id forKey:@"agent_id"];

    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"agent/achie";
    request.parms = dic;
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [self.tableView.mj_header endRefreshing];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                if ([[dataModel.data allKeys]containsObject:@"achievement"]) {
                    NSDictionary *achievement = [dataModel.data valueForKey:@"achievement"];
                    self.achievement = achievement;
                    
                    if (self.achievement && [[self.achievement allKeys]containsObject:@"tradeTotal"]) {
                        NSString *tradeTotal = [NSString stringWithFormat:@"%@",[self.achievement valueForKey:@"tradeTotal"]];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"kHoemAgentDetailstradeTotalNotificationID" object:tradeTotal];
                    }
                    if ([[dataModel.data allKeys]containsObject:@"name"]) {
                        NSString *name = [NSString stringWithFormat:@"%@",[dataModel.data valueForKey:@"name"]];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"kHoemAgentDetailstradeNameNotificationID" object:name];
                    }
                }
                
                [self.tableView reloadData];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHHoemAgentDetailsSubCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHHoemAgentDetailsSubCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"时间"]) {
        [self setCellTimeWith:cell.contentView title:self.date];
    }else if ([title isEqualToString:@"机具总数"]) {
        NSString *content = @"0";
        if (self.achievement && [[self.achievement allKeys]containsObject:@"countTerm"]) {
            content = [NSString stringWithFormat:@"%@",[self.achievement valueForKey:@"countTerm"]];
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES];
    }else if ([title isEqualToString:@"装机机具数"]) {
        NSString *content = @"0";
        if (self.achievement && [[self.achievement allKeys]containsObject:@"installTerms"]) {
            content = [NSString stringWithFormat:@"%@",[self.achievement valueForKey:@"installTerms"]];
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES ];
    }else if ([title isEqualToString:@"库存机具数"]) {
        NSString *content = @"0";
        if (self.achievement && [[self.achievement allKeys]containsObject:@"uninstallTerms"]) {
            content = [NSString stringWithFormat:@"%@",[self.achievement valueForKey:@"uninstallTerms"]];
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES];
    }else if ([title isEqualToString:@"代理总数"]) {
        NSString *content = @"0";
        if (self.achievement && [[self.achievement allKeys]containsObject:@"agentCount"]) {
            content = [NSString stringWithFormat:@"%@",[self.achievement valueForKey:@"agentCount"]];
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES];
    }else if ([title isEqualToString:@"商户总数"]) {
        NSString *content = @"0";
        if (self.achievement && [[self.achievement allKeys]containsObject:@"customerCount"]) {
            content = [NSString stringWithFormat:@"%@",[self.achievement valueForKey:@"customerCount"]];
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    WS(weakSelf)
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"时间"]) {
        if (self.type == 1) {
            NSString *cuDate = [NSString currentTimeWith:@"yyyy"];
            NSMutableArray *dicts = [NSMutableArray array];
            int dateInt = [cuDate intValue];
            for (int i = 0; i < 10; i++) {
                dateInt -= 1;
                [dicts addObject:@{@"color":@"#373737",@"title":[NSString stringWithFormat:@"%d",dateInt]}];
            }
            HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"选择时间"];
            [menu show];
            menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
                weakSelf.date = title;
                [weakSelf.tableView.mj_header beginRefreshing];
            };
        }else {
            [self.dateAlert show];
        }
    }
}

- (id<YWAlertViewProtocol>)dateAlert{
    if (!_dateAlert) {
        WS(weakSelf)
        YWAlertPublicBodyStyle bodyStyle;
        if (self.type == 1) {

        }else if (self.type == 2) {
            bodyStyle = YWAlertStyleShowYearMonth;
        }else if (self.type == 3) {
            bodyStyle = YWAlertStyleShowYearMonthDay;
        }
        _dateAlert = [YWAlertView alertViewWithTitle:@"" preferredStyle:YWAlertViewStyleDatePicker2 footStyle:YWAlertPublicFootStyleDefalut bodyStyle:bodyStyle cancelButtonTitle:@"取消" sureButtonTitles:@"确定" handler:^(NSInteger buttonIndex, id  _Nullable value) {
            if (!value || buttonIndex == 100) {
                return;
            }
            weakSelf.date = [NSString stringWithFormat:@"%@",value];
            [weakSelf.tableView.mj_header beginRefreshing];
        }];
    }
    return _dateAlert;
}


-(void)setCellConWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isline {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor whiteColor];

    
    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"111a34" text:title alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(100);
    }];
    
    UILabel *conLabel = [UILabel ha_NLabelWithFont:15 textColor:@"858b9c" text:content alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(titlabel.mas_right).offset(15);
        make.right.mas_equalTo(-20);
    }];
    
    if (isline == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(1);
            make.bottom.mas_equalTo(0);
        }];
    }
}

-(void)setCellTimeWith:(UIView *)contentView title:(NSString *)title {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"#666666" text:title alignment:2];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
    }];

}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}



@end
